/**
 * HOMER Core - an open and flexible OSGi-based software platform for SmartHome and AAL applications
 *
 * Copyright (C) 2011-2013 AIT Austrian Institute of Technology GmbH - <http://www.ait.ac.at>
 * Contact: Thomas Fuxreiter <thomas.fuxreiter@ait.ac.at>
 *
 * This file is part of HOMER Core.
 *
 * HOMER Core is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * HOMER Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package at.aal2aac.eventbusclient;

public class EventbusMessage<T> {
	
	public static final String REGISTER = "register";
	public static final String UNREGISTER = "unregister";
	public static final String PUBLISH = "publish";
	public static final String SEND = "send";
	public static final String PING = "ping";
	public static final String PINGREPLY = "pingreply";
	public static final String UNKNOWN = "unknown";

	private Class<T> bodyType = null;
		
	private String address;
	private String replyAddress;
	private T body;
	private String mode;
	private String senderId = "default";
	
	/**
	 * Default constructor for EventbusMessage, needed for serialization. Do not use!
	 */
	
	public EventbusMessage() {
	}
	
	/**
	 * Create a new Eventbus Message.
	 * @param senderId - ID of the sender
	 * @param address - destination address of the message or address for registration
	 * @param m - Mode 
	 */
	public EventbusMessage(final String senderId, final String address, final String m) {
		 this.address = address;
		 this.mode = m;
		 this.senderId = senderId;
	}
	/**
	 * Create a new Eventbus Message.
	 * @param senderId - ID of the sender
	 * @param address - destination address of the message or address for registration
	 * @param m - Mode
	 * @param body - The body object, preferably a POJO object for straight forward serialization 
	 */
	
	public EventbusMessage(final String senderId, final String address, final String m, final T body) { 
		 this.senderId = senderId;
		 this.address = address;
		 this.mode = m;
		 setBody(body);
	}
		
	
		
    public void setSenderId(final String senderId) {
    	this.senderId = senderId;
    }
    
    public String getSenderId() {
    	return this.senderId;
    }
	
	public void setMode(final String m) {
	    this.mode = m;
	}
	
	public String getMode() {
		return mode;
	}
	
	public void setAddress(final String a) {
		this.address = a;
	}
	
	public String getAddress() {
		return this.address;
	}

	public String getReplyAddress() {
		return replyAddress;
	}

	public void setReplyAddress(final String replyAddress) {
		this.replyAddress = replyAddress;
	}

	public T getBody() {
		return body;
	}

	@SuppressWarnings("unchecked")
	public void setBody(final T message) {
		if (message == null) {
			return;
		}
		this.bodyType = (Class<T>) message.getClass();
		this.body = message;
	}
	
	public Class<T> getBodyType() {
		return bodyType;
	}
	
	
}
